﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using OrderManage.Model;
using OrderManage.Utility;

namespace OrderManage.DAL
{
    public class MenuDAL : DbHelperBase
    {
        public IList<MenuInfo> LoadMenuList(string roleId)
        {
            var strSql = new StringBuilder();
            var list = new List<MenuInfo>();
            object obj = DBNull.Value;
            strSql.Append("SELECT A.MenuID,A.MenuName,A.URL,A.ParentMenuID FROM UT_SysMenu AS A ");
            strSql.Append(" INNER JOIN UT_RoleRight AS B ON A.MenuID = B.MenuID  ");
            strSql.Append(" WHERE B.RoleID=@RoleID ORDER BY A.[MenuIDX] ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@RoleID", roleId);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new MenuInfo();
                            if (reader["MenuID"] != DBNull.Value)
                                model.MenuID = new Guid(reader["MenuID"].ToString());
                            if (reader["ParentMenuID"] != DBNull.Value)
                                model.ParentMenuID = new Guid(reader["ParentMenuID"].ToString());
                            model.MenuName = reader["MenuName"] == obj ? null : Convert.ToString(reader["MenuName"]);
                            model.URL = reader["URL"] == obj ? null : Convert.ToString(reader["URL"]);
                            list.Add(model);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
    }
}
